package com.burgos.inventory;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import org.json.JSONObject;
import com.google.gson.Gson;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Statistics extends Activity implements OnClickListener {

	private TextView t;
	private Intent i;
	private ProgressDialog pDialog3;
	private static final String READ_STATS = "http://test1984.site11.com/remote/getstats.php";
	
	private Button waitingB;
	private Button officeB;
	private Button customsB;
	private Button transitB;
	
	private List<StatDetails> stats;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_statistics);
		
		waitingB = (Button) findViewById(R.id.viewwaiting);
		officeB = (Button) findViewById(R.id.viewoffice);
		customsB = (Button) findViewById(R.id.viewcustoms);
		transitB = (Button) findViewById(R.id.viewtransit);
		
		waitingB.setOnClickListener(this);
		officeB.setOnClickListener(this);
		customsB.setOnClickListener(this);
		transitB.setOnClickListener(this);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		new loadStats().execute();
		
	}
	
	@Override
	public void onClick(View v) {
			switch (v.getId()) {
			case R.id.viewwaiting:
				i = new Intent(this, Product.class);
				i.putExtra("keyword","Not yet in USA");
				finish();
				startActivity(i);
				break;
			case R.id.viewoffice:
				i = new Intent(this, Product.class);
				i.putExtra("keyword","In USA office");
				finish();
				startActivity(i);
				break;
			case R.id.viewcustoms:
				i = new Intent(this, Product.class);
				i.putExtra("keyword","At Bolivia - Customs");
				finish();
				startActivity(i);
				break;
			case R.id.viewtransit:
				i = new Intent(this, Product.class);
				i.putExtra("keyword","Sent to Bolivia");
				finish();
				startActivity(i);
				break;
			default:
				break;
			}
	}
	
	
	private void updateStats() {
			t=(TextView)findViewById(R.id.statisticshead); 
			t.setText("Products to arrive: " + stats.get(0).getTAG_TOAARIVE());
			t=(TextView)findViewById(R.id.atoffice); 
			t.setText("Products at Office: " + stats.get(0).getTAG_ATOFFICE());
			t=(TextView)findViewById(R.id.customs); 
			t.setText("Products at Customs: " + stats.get(0).getTAG_ATCUSTOMS());
			t=(TextView)findViewById(R.id.intransit); 
			t.setText("Products in Transit: " + stats.get(0).getTAG_INTRANSIT());
	}
	
	public class loadStats extends AsyncTask<String, String, String> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog3 = new ProgressDialog(Statistics.this);
			pDialog3.setMessage("Retrieving Statistics...");
			pDialog3.setIndeterminate(false);
			pDialog3.setCancelable(true);
			pDialog3.show();
		}

		@Override
		protected String doInBackground(String... args){
			//get stats
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair("keyword", "stats"));
				// making HTTP request and getting the response
				// SSL
				JSONParser jsonParser = new JSONParser();
				JSONObject json = jsonParser.makeHttpRequest(READ_STATS, "POST", params);

				String det = json.toString();
				Log.i("test1",det);
				
				//Using GSON to build the list of products
				Gson gson = new Gson();
			    
				ListofStats data = gson.fromJson(det, ListofStats.class);
			    Log.i("test2", data.toString());
			    
			    stats = data.getStats();
				return null;
		}
		
		@Override
		protected void onPostExecute(String file_url) {
			pDialog3.dismiss();
			updateStats();
		}
	}
}

